import { formatDate } from '@/filters/index'
import func from '@/util/func';
export const line1Option = (records) => {
  let xAxis = [] , d = new Date() , data = [[] , []];
  for(let i = 0; i < records.length; i++){
    let item = records[i];
    xAxis.push(item['dataDate']);
    data[0].push(item['totalRecord']);
    data[1].push(item['totalAlarm']);
  }
  return {
    grid: {
      containLabel: true,
      left: 5,
      right: 5,
      top: 30,
      bottom: 5
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
        data: ['数据量', '警情(个)'],
        show: false
    },
    xAxis: [
        {
          type: 'category',
          data: xAxis,
          // axisPointer: {
          //   type: 'shadow'
          // }
        }
    ],
    yAxis: [
        {
            type: 'value',
            name: '数据量(万条)',
            min: 0,
            axisLabel: {
              formatter: function (item) {
                return func.numberFormat(item,0).value
              }
            }
        },
        {
            type: 'value',
            name: '警情(个)',
            min: 0,
            // axisLabel: {
            //   formatter: '{value} °C'
            // }
        }
    ],
    series: [
      {
        name: '数据量',
        type: 'line',
        data: data[0]
      },
      {
        name: '警情数',
        type: 'line',
        yAxisIndex: 1,
        data: data[1]
      }
    ]
  }
}

export const barOption = (xData,yData) => {
  return {
    grid: {
      containLabel: true,
      left: 5,
      right: 5,
      top: 15,
      bottom: 5
    },
    tooltip: {},
    yAxis: {
      type: 'value'
    },
    xAxis: {
      type: 'category',
      axisLabel: {
        interval:0,
        rotate: 30
      },
      data: xData
    },
    series: [
      {
        data: yData,
        type: 'bar',
        barWidth: 15
      }
    ]
  }
}

export const pieOption = (data , formatter , opt) => {
  return {
      ...opt,
      tooltip: {
        trigger: 'item',
        formatter: '{b}: {c} ({d}%)',
        backgroundColor: "rgba(255,255,255,0.75)",
        textStyle: {
          color: '#000'
        }
      },
      legend: {
        orient: 'vertical',
        left: 0,
        top: 'center',
        data: data.legend,
        icon: 'circle',
        formatter: formatter || function(name){
          return `${name}  {value|${data.legendDatas[name]['value']}, ${data.legendDatas[name]['pre']}%}`;
        },
        textStyle: {
          rich: {
            value: {
              color: '#999'
            }
          }
        }
      },
      series: data.series ? data.series : [
        {
          type: 'pie',
          radius: ['40%' , '60%'],
          center: ['75%' , '50%'],
          label: {
            show: false
          },
          labelLine: {
            show: false
          },
          data: data.data
        }
      ]
  }
}

export const getDataCollectCountStr = (num , unit) => {
  unit = unit || '条';
  if(num > 99999999){
    return (num / 100000000).toFixed(2) + '亿' + unit;
  }else if(num < 100000000 && num > 9999){
    return (num / 100000000).toFixed(2) + '万' + unit;
  }else{
    return num + unit;
  }
}

export const createTestData = (legends) => {
  let data = [];
  legends.forEach(
    item => {
      data.push({name: item , value: Math.ceil(Math.random() * 10000)})
    }
  )
  return data;
}

export const getPre = (count , values) => {
  Object.keys(values).forEach(
    key => {
      let value = values[key].c;
      let pre = (value / count * 100);
      values[key]['pre'] = Number(pre.toString().match(/^\d+(?:\.\d{0,2})?/));
    }
  )
  return values;
}
